package controller

import (
	"fmt"
	"go_web/src/domain"
	"go_web/src/services"

	"github.com/gin-gonic/gin"
)

type OrderController struct {
	us services.OrderService
}

func NewOrderController(us services.OrderService) *OrderController {
	return &OrderController{us: us}
}

func (uc OrderController) CreateOrder(c *gin.Context) {
	fmt.Printf("完整请求路径是：%s", c.FullPath())

	//body参数
	var Order domain.Order
	err := c.ShouldBindJSON(&Order)
	if err != nil {
		c.JSON(200, gin.H{
			"code":    500,
			"message": "示例数据",
			"data":    "",
		})
		return
	}

	er := uc.us.Create(&Order)
	if er != nil {
		return
	}

	data := gin.H{
		"id":      1,
		"title":   "1.0",
		"name":    "张三",
		"message": "示例数据",
	}

	// 根据 Accept 头返回不同格式
	switch c.GetHeader("Accept") {
	case "application/xml":
		c.XML(200, data)
	case "application/yaml":
		c.YAML(200, data)
	case "text/html":
		c.HTML(200, "index.html", data)
	default:
		c.JSON(200, data) // 默认返回 JSON
	}
}

func (uc OrderController) GetOrder(c *gin.Context) {
	//查询参数
	orderNum := c.DefaultQuery("orderNum", "1")
	fmt.Println(orderNum)

	Order, _ := uc.us.FindByOrderNum(orderNum)
	c.JSON(200, gin.H{
		"code":    500,
		"message": "示例数据",
		"data":    Order,
	})
}
